﻿using System;
using System.Net;
using System.Windows;
using System.IO;
using System.Text;
using Windows.Security.Cryptography.Core;
using Windows.Security.Cryptography;

namespace MetroDropBox.Shared
{
    public static class SHA1EncryptionHelper
    {
        /// <summary>
        /// Helper method to sign a string with a key using HMAC-Sha1
        /// </summary>
        /// <param name="key"></param>
        /// <param name="signThis"></param>
        /// <returns></returns>
        /// <remarks>Thank you Tim Heuer for the code in the MS WebAuthentication Sample</remarks>
        public static String HMACSha1Sign(String key, String signThis)
        {
            try
            {
                var keyBuffer = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
                var crypto = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1);    
                var cryptoKey = crypto.CreateKey(keyBuffer);
                var dataBuffer = CryptographicBuffer.ConvertStringToBinary(signThis, BinaryStringEncoding.Utf8);
                var signatureBuffer = CryptographicEngine.Sign(cryptoKey, dataBuffer);
                var result = CryptographicBuffer.EncodeToBase64String(signatureBuffer);

                return result;
            }
            finally
            {
            }
            return "";
        }
    }
}
